package com.moyu.oasys.attendance.service.impl;

import com.moyu.common.core.enums.ResultCode;
import com.moyu.common.security.exception.ServiceException;
import com.moyu.oasys.attendance.entity.Attendance;
import com.moyu.oasys.attendance.mapper.AttendanceMapper;
import com.moyu.oasys.attendance.service.IAttendanceService;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

/**
 * @Author 赵彦博
 * @Date 2024/12/2 20:19
 */
@Service
public class AttendanceServiceImpl implements IAttendanceService {

    private final AttendanceMapper attendanceMapper;

    public AttendanceServiceImpl(AttendanceMapper attendanceDao) {
        this.attendanceMapper = attendanceDao;
    }

    @Override
    public List<Attendance> listByUserIdAndClockDate(Long userId, Integer year, Integer month, Integer day) {
        return attendanceMapper.listByUserIdAndClockDate(userId, year, month, day);
    }

    @Override
    public Attendance create(Long userId) throws ServiceException {
        LocalDateTime nowDateTime = LocalDateTime.now();
        int year = nowDateTime.getYear();
        int month = nowDateTime.getMonthValue();
        int day = nowDateTime.getDayOfMonth();
        if (attendanceMapper.listByUserIdAndClockDate(userId, year, month, day).size() > 0) {
            throw new ServiceException(ResultCode.TODAY_HAS_CLOCKED_IN);
        }
        Attendance attendance = new Attendance();
        attendance.setUserId(userId);
        String workingHoursStart = attendanceMapper.selectSettingValueByName("working_hours_start");
        Duration duration = Duration.between(LocalTime.parse(workingHoursStart), LocalTime.now());
        long lateMinutes = duration.toMinutes();
        if (lateMinutes > 0) {
            attendance.setComeLateMinutes((int) lateMinutes);
        }
        attendance.setClockDate(LocalDate.now());
        attendance.setClockInTime(LocalTime.now());
        attendanceMapper.insert(attendance);
        return attendance;
    }

    @Override
    public Attendance update(Long id) throws ServiceException {
        Attendance attendance = attendanceMapper.selectById(id);
        if (attendance == null) {
            throw new ServiceException(ResultCode.TODAY_HAS_NOT_CLOCKED_IN);
        }
        if (attendance.getClockOutTime() != null) {
            throw new ServiceException(ResultCode.TODAY_HAS_CLOCKED_OUT);
        }
        String workingHoursEnd = attendanceMapper.selectSettingValueByName("working_hours_end");
        Duration duration = Duration.between(LocalTime.now(), LocalTime.parse(workingHoursEnd));
        long earlyMinutes = duration.toMinutes();
        if (earlyMinutes > 0) {
            attendance.setLeaveEarlyMinutes((int) earlyMinutes);
        }
        attendance.setClockOutTime(LocalTime.now());
        attendanceMapper.updateById(attendance);
        return attendanceMapper.selectById(attendance.getId());
    }

}
